package com.cr.web.controller.lsd;

import com.cr.common.core.domain.AjaxResult;
import com.cr.common.core.domain.model.LoginUser;
import com.cr.framework.web.service.SysLoginService;
import com.cr.framework.web.service.TokenService;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.validation.constraints.NotBlank;
import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/lsd")
@Validated
public class LoginController {

    @Resource
    private SysLoginService loginService;

    @Resource
    private TokenService tokenService;

    /**
     * 大屏登录
     */
    @PostMapping("/login")
    public AjaxResult login(@NotBlank(message = "用户名不能为空") String username,
                            @NotBlank(message = "密码不能为空") String password)
    {
        // 生成令牌
        String token = loginService.login(username, password, null, null, false);
        LoginUser loginUser = tokenService.getLoginUser(token);

        // 返回结果
        Map<String, Object> result = new HashMap<>();
        result.put("token", token);
        result.put("nickName", loginUser.getUser().getNickName());
        return AjaxResult.success(result);
    }

}
